using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Entities;
using BUS;
using DAL;
using Utility;
public partial class Admin_D_Themsanphamt : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Url.ToString().Contains("dangnhapdoanhnghiep.aspx") == false
&& Session["Tendangnhap"] == null)
            Response.Redirect("~/Admin/dangnhapdoanhnghiep.aspx");
        if (!IsPostBack)
        {
            loaddropdownlist();
        }
    }
    private void loaddropdownlist()
    {
        //dr_tendn.DataSource = tbDoanhnghiepBUS.GetAll();
        //dr_tendn.DataTextField = tbDoanhnghiepColumn.Tendnghiep.ToString();
        //dr_tendn.DataValueField = tbDoanhnghiepColumn.Madn.ToString();
        //dr_tendn.DataBind();
        ////dr_tendn.Items.Insert(0, new ListItem("All", "0"));

        dr_tendm.DataSource = tbDanhmucBUS.GetAll();
        dr_tendm.DataTextField = tbDanhmucColumn.Tendanhm.ToString();
        dr_tendm.DataValueField = tbDanhmucColumn.Madanhm.ToString();
        dr_tendm.DataBind();
        dr_tendm.Items.Insert(0, new ListItem("All", "0"));

        dr_tenkv.DataSource = tbKhuvucBUS.GetALL();
        dr_tenkv.DataTextField = tbKhuvucColumn.Tenkv.ToString();
        dr_tenkv.DataValueField = tbKhuvucColumn.Makv.ToString();
        dr_tenkv.DataBind();
        dr_tenkv.Items.Insert(0, new ListItem("All", "0"));

        dr_tt.DataSource = tbTrangthaisanphamBUS.GetAll();
        dr_tt.DataTextField = tbTrangthaispColumn.Trangthai.ToString();
        dr_tt.DataValueField = tbTrangthaispColumn.Mattsp.ToString();
        dr_tt.DataBind();
        dr_tt.Items.Insert(0, new ListItem("All", "0"));
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string mdn = Session["madn"].ToString();
        //try
        //{
            tbSanpham sp = new tbSanpham();
            sp.Tensp = txttensp.Text;
            sp.Madn = int.Parse(mdn.ToString());
            sp.Madmuc = int.Parse(dr_tendm.SelectedValue);
            sp.Makv = int.Parse(dr_tenkv.SelectedValue);
            sp.Mattsp = int.Parse(dr_tt.SelectedValue);
            sp.Giaban = float.Parse(txt_gban.Text);
            sp.Giagoc = float.Parse(txt_ggoc.Text);
            sp.Mota = txt_mota.Text;
            sp.Thoigianbd = DateTime.Parse(tg_bd.Text);
            sp.Thoigiankt = DateTime.Parse(tg_kt.Text);
            sp.Tilechietkhau = txt_tlck.Text;
            sp.Soluong = int.Parse(txt_sluong.Text);
            sp.Dieukien = txt_dk.Text;
            sp.Hansudung = DateTime.Parse(txt_hsd.Text);
            sp.Soluongban = int.Parse(txt_slb.Text);
            sp.Lido = txt_ld.Text;
            if (img_bdo.HasFile)
            {
                string filename = DateTime.Now.Ticks.ToString() + "_" + img_bdo.FileName;
                img_bdo.SaveAs(Server.MapPath("~/Hinhanh/" + filename));
                System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath("~/Hinhanh/" + filename));
                ImageUtility.MakeThumbnailPhoto(img, 100, 100, Server.MapPath("~/Hinhanh/Hinh/"), filename);
                sp.Bando = filename;
            }
            else
            {
                sp.Bando = "no_images.jpg";
            }

            if (img_sp.HasFile)
            {
                string filename = DateTime.Now.Ticks.ToString() + "_" + img_sp.FileName;
                img_sp.SaveAs(Server.MapPath("~/Hinhanh/" + filename));
                System.Drawing.Image img = System.Drawing.Image.FromFile(
                    Server.MapPath("~/Hinhanh/" + filename));
                ImageUtility.MakeThumbnailPhoto(
                    img, 100, 100, Server.MapPath("~/Hinhanh/Hinh/"), filename);
                sp.Hinhchinh = filename;
            }
            else
            {
                sp.Hinhchinh = "no_images.jpg";
            }
            tbSanphamBUS.Insert(sp);

            Response.Redirect("~/Admin/D_Sanphamdoanhnghiep.aspx?id="+ mdn.ToString());
        //}
        //catch
        //{
        //    Response.Redirect("~/Admin/D_Sanphamdoanhnghiep.aspx?id=" + mdn.ToString());
        //}
    }
}
